package controllers;


import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.SolutionSubmission;
import models.Benefit;
import models.SolutionAdoption;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;


	public class ByBenefitsMainConnect{
		
	int iROWS;
	Boolean hasEntries;
	public List<HashMap<String, Object>> getQueryFinancial(String fieldName, String Value) {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String, Object>>();
		Objectify ofy1 = ObjectifyService.begin();
		
		try {
			
			
			
			Query<Benefit> q = ofy1.query(Benefit.class).filter(fieldName, Value);
			for (Benefit f : q.fetch()) {
				HashMap<String, Object> itemMap = new HashMap<String, Object>();				
				itemMap.put("benefitName", f.getBenefitName());
				String fname = "has"+ f.getBenefitName().replace(" ", "");
										
				try{
					Objectify ofy2 = ObjectifyService.begin();
					String BenefitName = f.getBenefitName(); //ADDED BY SRVAN ON 19 SEPT
					Query<SolutionSubmission> a = null;
					if ( BenefitName.equalsIgnoreCase("Hard Savings") ){
						a = ofy2.query(SolutionSubmission.class).filter("hasHardSavings",true).filter("Status", "Shared");
					}else if ( BenefitName.equalsIgnoreCase("Cost Avoidance") ){
						a = ofy2.query(SolutionSubmission.class).filter("hasCostAvoidance",true).filter("Status", "Shared");
					}else if ( BenefitName.equalsIgnoreCase("Opportunity Costs") ){
						a = ofy2.query(SolutionSubmission.class).filter("hasOppurtunityCost",true).filter("Status", "Shared");
					}
					
					itemMap.put("CountSoShare", a.countAll());
					
					Objectify ofy3 = ObjectifyService.begin();
					Query<SolutionAdoption> b = null;					
					if ( BenefitName.equalsIgnoreCase("Hard Savings") ){
						b = ofy3.query(SolutionAdoption.class).filter("hasHardSavings",true);
					}else if ( BenefitName.equalsIgnoreCase("Cost Avoidance") ){
						b = ofy3.query(SolutionAdoption.class).filter("hasCostAvoidance",true);
					}else if ( BenefitName.equalsIgnoreCase("Opportunity Costs") ){
						b = ofy3.query(SolutionAdoption.class).filter("hasOppurtunityCost",true);
					}					
					itemMap.put("CountAdopShare", b.countAll());
				
				}catch (Exception e) {
					e.printStackTrace();
				}
				objList.add(itemMap);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return objList;
	}
	
	public List<HashMap<String, Object>> getQueryAchieved(String fieldName, String Value) {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String, Object>>();
		Objectify ofy1 = ObjectifyService.begin();
		
		try {
			
			
			
			Query<Benefit> q = ofy1.query(Benefit.class).filter(fieldName, Value);
			for (Benefit f : q.fetch()) {
				HashMap<String, Object> itemMap = new HashMap<String, Object>();				
				itemMap.put("benefitName", f.getBenefitName());
										
				try{
					Objectify ofy2 = ObjectifyService.begin();
					Query<SolutionSubmission> a = ofy2.query(SolutionSubmission.class).filter("achievedTypeDesc",f.getBenefitName()).filter("Status", "Shared");
					itemMap.put("CountSoShare", a.countAll());
					
					Objectify ofy3 = ObjectifyService.begin();
					Query<SolutionAdoption> b = ofy3.query(SolutionAdoption.class).filter("achievedTypeDesc",f.getBenefitName());
					itemMap.put("CountAdopShare", b.countAll());
					
				
				}catch (Exception e) {
					e.printStackTrace();
				}
				objList.add(itemMap);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return objList;
	}
	

}

		
